﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Reflection;
using System.IO;
using System.Net;
using ThreeByte.Logging;

namespace ThreeByte.ActiveDeck.Service
{
    public static class EmailUtil
    {
        private static readonly NetworkCredential EMAIL_CREDENTIALS = new NetworkCredential("info@activedeck.com", "admin0101!");
        private static readonly string MAIL_HOST = @"mail.activedeck.com";
        private static readonly MailAddress ACTIVEDECK_INFO = new MailAddress("info@activedeck.com", "ActiveDeck Info");
        private static readonly MailAddress ACTIVEDECK_SUPPORT = new MailAddress("support@activedeck.com", "ActiveDeck Support");
        private static readonly MailAddress ACTIVEDECK_ADDIN = new MailAddress("addin@activedeck.com", "ActiveDeck AddIn Reporting");
        private static readonly MailAddress REDMINE_REPORTING = new MailAddress("sql@threebyteintermedia.com", "Redmine Reporting");

        public static void SendInvitationEmail(string emailAddress, Guid key) {
            MailAddress fromAddress = new MailAddress("info@activedeck.com", "ActiveDeck Info");
            MailAddress toAddress = new MailAddress(emailAddress);
            MailMessage message = new MailMessage(fromAddress, toAddress);
            message.Subject = "ActiveDeck Download";
            message.Body = GetInvitationEmailBody(key);
            message.IsBodyHtml = true;

            //ActiveDeck email relay
            SmtpClient mailClient = new SmtpClient(MAIL_HOST);
            mailClient.Credentials = EMAIL_CREDENTIALS;

            mailClient.Send(message);
        }

        public static string GetDownloadLink(Guid key) {
            return string.Format("{0}/Config.svc/Download?x={1}", ServiceConfiguration.SERVICE_HOST, key);
        }

        private static string GetInvitationEmailBody(Guid key) {

            string downloadLink = GetDownloadLink(key);

            string body = GetResourceString("Invitation_Email.html");
            body = body.Replace("***DOWNLOAD_LINK***", downloadLink);
            //TODO: Add a link to sign in to the console

            return body;
        }

        private static string GetResourceString(string name) {
            Assembly thisAssembly = Assembly.GetExecutingAssembly();
            StreamReader reader = new StreamReader(thisAssembly.GetManifestResourceStream("ThreeByte.ActiveDeck.Service.Email." + name));
            return reader.ReadToEnd();
        }


        public static void SendVerificationEmail(string emailAddress, Guid key) {
            MailAddress fromAddress = new MailAddress("info@activedeck.com", "ActiveDeck Info");
            MailAddress toAddress = new MailAddress(emailAddress);
            MailMessage message = new MailMessage(fromAddress, toAddress);
            message.Subject = "ActiveDeck Email Verification";
            message.Body = GetVerificationEmailBody(key);
            message.IsBodyHtml = true;

            //ActiveDeck email relay
            SmtpClient mailClient = new SmtpClient(MAIL_HOST);
            mailClient.Credentials = EMAIL_CREDENTIALS;

            mailClient.Send(message);
        }

        private static string GetVerificationEmailBody(Guid key) {

            string downloadLink = GetVerificationLink(key);

            string body = GetResourceString("Verification_Email.html");
            body = body.Replace("***VERIFICATION_LINK***", downloadLink);
            //TODO: Add a link to sign in to the console

            return body;
        }

        public static string GetVerificationLink(Guid key) {
            return string.Format("{0}/console/Verify.aspx?x={1}", ServiceConfiguration.SERVICE_HOST, key);
        }



        public static void SendPasswordResetEmail(string emailAddress, Guid key) {
            MailAddress fromAddress = new MailAddress("info@activedeck.com", "ActiveDeck Info");
            MailAddress toAddress = new MailAddress(emailAddress);
            MailMessage message = new MailMessage(fromAddress, toAddress);
            message.Subject = "ActiveDeck Password Reset";
            message.Body = GetPasswordResetEmailBody(key);
            message.IsBodyHtml = true;

            //ActiveDeck email relay
            SmtpClient mailClient = new SmtpClient(MAIL_HOST);
            mailClient.Credentials = EMAIL_CREDENTIALS;

            mailClient.Send(message);
        }

        private static string GetPasswordResetEmailBody(Guid key) {

            string downloadLink = GetPasswordResetLink(key);

            string body = GetResourceString("Password_Reset_Email.html");
            body = body.Replace("***PASSWORD_RESET_LINK***", downloadLink);
            
            return body;
        }

        public static string GetPasswordResetLink(Guid key) {
            return string.Format("{0}/console/ResetPassword.aspx?x={1}", ServiceConfiguration.SERVICE_HOST, key);
        }


        public static void SendErrorReportEmail(string reportId, string reportSummary, Stream attachment = null) {
            MailMessage message = new MailMessage(ACTIVEDECK_ADDIN, ACTIVEDECK_SUPPORT);
            message.To.Add(REDMINE_REPORTING);
            message.Subject = string.Format("ActiveDeck Error Report: {0}", reportId);

            StringBuilder sb = new StringBuilder();
            sb.AppendLine("Project: activedeck\nStatus: New\nPriority: High\nTracker: Bug\n");
            sb.AppendFormat("An error has been reported.  Please use the following ID for further information: {0}\n", reportId);
            sb.AppendFormat("Error Detail Link:  https://svc.activedeck.com/console/#/Error/{0}\n", reportId);
            sb.AppendLine();
            sb.AppendFormat("Summary:\n{0}\n", reportSummary);
            
            message.Body = sb.ToString();
            message.IsBodyHtml = false;

            if(attachment != null){
            //Create a new attachment
                Attachment a = new Attachment(attachment, "LogFile.txt");
                message.Attachments.Add(a);
            }

            //ActiveDeck email relay
            SmtpClient mailClient = new SmtpClient(MAIL_HOST);
            mailClient.Credentials = EMAIL_CREDENTIALS;

            mailClient.Send(message);
        }
    }
}
